/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ebm;

/**
 *
 * @author vincenzoscognamiglio
 */
public class LevenshteinMethod {

    /*  public void createHashmapContrazioni() {
    Iterator<String> itSt = arrStr.iterator();

    while (itSt.hasNext()) {
    String word = itSt.next();
    if (word.length() > 4) {
    for (int cn = 1; cn < word.length() - 1; cn++) {

    if (!Tools.isVocal(word.charAt(cn)) && (!Tools.isVocal(word.charAt(cn - 1)) || !Tools.isVocal(word.charAt(cn + 1)))) {
    System.out.println("Sostituizione saltata di :" + word + "  -->" + word.substring(0, cn) + word.substring(cn + 1, word.length()));
    } else {
    String str = word.substring(0, cn) + word.substring(cn + 1, word.length());

    System.out.println("Sostituibili di:" + word + "-->" + str);
    contrazioni.put(str, word);
    }
    }
    }
    }
    }*/
    /*Algoritmo generico di Levenshtein*/
    private static int minimum(int a, int b, int c) {
        return Math.min(Math.min(a, b), c);
    }

    public static int computeLevenshteinDistance(CharSequence str1,
            CharSequence str2) {
        int[][] distance = new int[str1.length() + 1][str2.length() + 1];

        for (int i = 0; i <= str1.length(); i++) {
            distance[i][0] = i;
        }
        for (int j = 0; j <= str2.length(); j++) {
            distance[0][j] = j;
        }

        for (int i = 1; i <= str1.length(); i++) {
            for (int j = 1; j <= str2.length(); j++) {
                distance[i][j] = minimum(
                        distance[i - 1][j] + 1,
                        distance[i][j - 1] + 1,
                        distance[i - 1][j - 1]
                        + ((str1.charAt(i - 1) == str2.charAt(j - 1)) ? 0
                        : 1));
            }
        }

        return distance[str1.length()][str2.length()];
    }
}